package ai.service;

import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ETL类
 */
@Service
public class DataLoaderService {
    @Value("classpath:text-source.txt")
    private org.springframework.core.io.Resource txtResource;

    @Resource
    private VectorStore vectorStore;

    public void loadDocuments() {
        // 提取: 使用文本读取器
        TextReader txtReader = new TextReader(txtResource);
        List<Document> documents = txtReader.get();

        // 转换：按Token拆分
        TokenTextSplitter splitter = new TokenTextSplitter();
        List<Document> chunks = splitter.apply(documents);

        // 加载: 存储到ES向量数据库
        vectorStore.add(chunks);
    }
}